package com.buaa.xxx0320;

import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.io.File;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    private SharedPreferences pref;
    private SQLiteDatabase db;
    private XXXOpenHelper helper;
    private int i=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        File sdcardPath = Environment.getExternalStorageDirectory();
        Log.i("xxx", sdcardPath + "............");
        helper = new XXXOpenHelper(this,sdcardPath.getAbsolutePath()+"/xxx11.db",null,2);
        db = helper.getWritableDatabase();
        //db = SQLiteDatabase.openOrCreateDatabase(sdcardPath.getAbsolutePath()+"/xxx1.db",null);
    }

    public void storeToFile(View view) throws Exception{
        OutputStream out = openFileOutput("xxx data", MODE_PRIVATE | MODE_APPEND);

     //   ObjectOutputStream oout = new ObjectOutputStream(out);
     //   oout.writeObject(new Object());

        out.write("hello world,你好 世界".getBytes("utf-8"));
        out.close();
    }

    public void getFromFile(View view) throws Exception{
        InputStream in = openFileInput("xxx data");
        byte[] buf = new byte[in.available()];

//        int count = 0;
//        while ((count=in.read(buf))!=-1) {
//
//        }
        in.read(buf);
        String msg = new String(buf,"utf-8");
        Log.i("xxx", msg);
        in.close();
    }

    public void storeToPref(View view) {
        Set<String> set = new HashSet<String>();
        set.add("foot");
        set.add("basket");
        SharedPreferences.Editor editor = pref.edit();
        editor.putString("uname","xxx");
        editor.putInt("age", 100);
        editor.putStringSet("fav",set);
        editor.commit();
    }

    public void getFromPref(View view) {
        String uname = pref.getString("uname","null");
        int age = pref.getInt("age", -1);
        Set<String> fav = pref.getStringSet("fav",null);
        Log.i("xxx", uname + "........." + age);
        for (String key:fav) {
            Log.i("xxx", key);
        }
    }

    public void storeToDB(View view) {
        String sql = "insert into users (uname) values (?)";
        try {
            db.beginTransaction();
            db.execSQL(sql, new Object[]{"xxx" + i++});
            db.setTransactionSuccessful();
        } catch (Exception e) {

        }finally {
            db.endTransaction();
        }
    }

    public void getFromDB(View view) {
        String sql = "select _id, uname from users where _id>?";
        Cursor c = db.rawQuery(sql,new String[]{"2"});
        while(c.moveToNext()) {
            int id = c.getInt(0);
            String uname = c.getString(1);
            Log.i("xxx", id + "..........uname:"+uname);
        }
    }


}
